package com.yjf.ljr.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.adapter.CommonAdapter;
import com.yjf.ljr.adapter.CommonAdapter.HandleCallBack;
import com.yjf.ljr.dataclass.InvestDetailItemDataClass;
import com.yjf.ljr.dataclass.InvestDetailItemDataClass.InvestDetail;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.view.XListView;
import com.yjf.ljr.view.XListView.IXListViewListener;

public class InvestDetailActivity extends IjActivity {

	@ID("xlvInvestDetail")
	private XListView mXlvInvestDetail;

	private String mTradeId;
	private final int mPageSize = 20;
	private int mCurPage = 1;
	private boolean mIsGettingData = false;
	private CommonAdapter mCommonAdapter;
	private List<InvestDetail> mData = new ArrayList<InvestDetail>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_invest_detail);

		getIntentParams();
		initControls();
		loadingData(mPageSize, mCurPage, false, true);
	}

	private void getIntentParams() {
		Intent i = getIntent();
		mTradeId = i.getStringExtra("tradeId");
	}

	/** 初始化控件 **/
	private void initControls() {
		setLeftBtnClick();
		setTitleStr("投资记录");

		mCommonAdapter = new CommonAdapter(this, mData, R.layout.item_project_invest_detail, ViewHolder.class,
				mHandleCallBack);
		mXlvInvestDetail.setAdapter(mCommonAdapter);
		mXlvInvestDetail.setPullLoadEnable(true);
		mXlvInvestDetail.setPullRefreshEnable(true);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mXlvInvestDetail.setRefreshTime(sdf.format(new Date()));
		mXlvInvestDetail.mFooterView.hide(); // 隐藏更多
		mXlvInvestDetail.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				mCurPage = 1;
				loadingData(mPageSize, mCurPage, false, false);
			}

			@Override
			public void onLoadMore() {
				loadingData(mPageSize, ++mCurPage, true, false);
			}
		});
	}

	private void loadingData(int pageSize, int curPage, boolean isAdd, boolean showProgress) {
		if (mIsGettingData) {
			return;
		}

		if (showProgress) {
			showProgressDialog();
		}
		new InvestDetailTask(pageSize, curPage, isAdd).execute();
	}

	class InvestDetailTask extends AsyncTask<Void, Void, String> {

		private int pageSize;
		private int curPage;
		private boolean isAdd = false;
		private InvestDetailItemDataClass dc = new InvestDetailItemDataClass();

		public InvestDetailTask(int pageSize, int curPage, boolean isAdd) {
			super();
			this.pageSize = pageSize;
			this.curPage = curPage;
			this.isAdd = isAdd;
		}

		@Override
		protected void onPreExecute() {
			mIsGettingData = true;
		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject reqObj = new RequestObject();
			reqObj.method = "appInvestDetail";
			reqObj.map.put("pageSize", pageSize);
			reqObj.map.put("pageNumber", curPage);
			reqObj.map.put("tradeId", mTradeId);
			return requestDataAndGetErrorMsg(reqObj, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
			} else {
				if (!isAdd) {
					mData.clear();
				}
				if (dc.investList != null && dc.investList.size() > 0) {
					mData.addAll(dc.investList);
					mCommonAdapter.notifyDataSetChanged();

					if (mData.size() <= (dc.totalPage - 1) * pageSize) {
						mXlvInvestDetail.mFooterView.show();
					} else {
						mXlvInvestDetail.mFooterView.hide();
					}
				} else {
					if (!TextUtils.isEmpty(dc.msg) && dc.totalPage == 0) {
						showToast(dc.msg);
					}
				}
			}
			mXlvInvestDetail.stopRefresh();
			mXlvInvestDetail.stopLoadMore();
			dismissProgressDialog();
			mIsGettingData = false;
		}
	}

	public static class ViewHolder {
		TextView tvPerson;
		TextView tvMoney;
		TextView tvTime;
	}

	private HandleCallBack mHandleCallBack = new HandleCallBack() {

		@Override
		public void handle(int position, View convertView, ViewGroup parent, Object holder, Object item) {
			InvestDetail info = (InvestDetail) item;
			ViewHolder h = (ViewHolder) holder;
			AppUtil.setViewText(h.tvPerson, info.userName);
			AppUtil.setViewText(h.tvMoney, info.amount);
			AppUtil.setViewText(h.tvTime, info.createDate);
			if (position % 2 == 0) {
				h.tvPerson.setBackgroundColor(Color.parseColor("#FCEDDC"));
				h.tvMoney.setBackgroundColor(Color.parseColor("#FCEDDC"));
				h.tvTime.setBackgroundColor(Color.parseColor("#FCEDDC"));
			} else {
				h.tvPerson.setBackgroundColor(Color.parseColor("#FFFFFF"));
				h.tvMoney.setBackgroundColor(Color.parseColor("#FFFFFF"));
				h.tvTime.setBackgroundColor(Color.parseColor("#FFFFFF"));
			}
		}
	};
}
